var DefaultTeaList = [];
DefaultTeaList.push({type:"Black Tea", amount:"1 cup", strength:"normal", mins: 5, secs: 0});
DefaultTeaList.push({type:"Green Tea", amount:"1 cup", strength:"normal", mins: 3, secs: 0});
DefaultTeaList.push({type:"Gunpowder", amount:"1 cup", strength:"normal", mins: 1, secs: 0});
DefaultTeaList.push({type:"10 sec timer", amount:"1 cup", strength:"strong", mins: 0, secs: 10});



function TeaPickerAssistant() {
	
}


TeaPickerAssistant.prototype.setup = function() {
	this.getListFromCookie();

	this.controller.setupWidget("teaListWgt",
		this.listAtt = {
			itemTemplate: "tea-picker/teaPickerRowTemplate",
			listTemplate: "tea-picker/teaPickerListTemplate",
			swipeToDelete: true,
			renderLimit: 40,
			reorderable: true
		},
		this.listModel = {
			items: this.teaList
		}
	);

	this.controller.listen("teaListWgt", Mojo.Event.listTap, this.openMinder.bindAsEventListener(this));
	this.controller.listen("teaListWgt", Mojo.Event.listDelete, this.handleListDelete.bindAsEventListener(this));
	
	var cookie = new Mojo.Model.Cookie('Running');
	if(cookie.get() == undefined)
	{
		this.controller.setupWidget(Mojo.Menu.commandMenu, undefined, teaMenuAttr);
	}
	else
	{
		this.controller.setupWidget(Mojo.Menu.commandMenu, undefined, teaMenuAttrRunning);
	}
	this.controller.setupWidget(Mojo.Menu.appMenu, myMenuAttr, myMenuModel);
	
};

TeaPickerAssistant.prototype.activate = function(event) {
	this.getListFromCookie();
	
	$("programTitle").innerHTML = "Tea Minder";
	this.controller.modelChanged(this.listModel);
};

TeaPickerAssistant.prototype.getListFromCookie = function() {
	var cookie = new Mojo.Model.Cookie('TeaList');
	this.teaList = cookie.get();
	
	if(this.teaList == undefined)
	{
		Mojo.Log.info("using defalut");
		this.teaList = DefaultTeaList;
		cookie.put(this.teaList);
	}
};

TeaPickerAssistant.prototype.handleListDelete = function() {
	this.teaList.splice(event.index, 1);
	
	var cookie = new Mojo.Model.Cookie('TeaList');
	cookie.put(this.teaList);
	
	Mojo.Log.info(Object.toJSON(this.teaList));
};

TeaPickerAssistant.prototype.deactivate = function(event) {
	
};

TeaPickerAssistant.prototype.cleanup = function(event) {

};


TeaPickerAssistant.prototype.openMinder = function(event) {
	var tea = this.teaList[event.index];
	
	var runningInfo = new Object();
	runningInfo.type = tea.type;
	runningInfo.amount = tea.amount;
	runningInfo.strength = tea.strength;
	runningInfo.mins = tea.mins;
	runningInfo.secs = tea.secs;
	runningInfo.paused = true;
	
	var cookie = new Mojo.Model.Cookie('Running');
	cookie.put(runningInfo);
	
	Mojo.Controller.stageController.swapScene("tea-minder");
};
